package wblog.web;

import cn.webwheel.Action;
import cn.webwheel.WebParam;
import cn.webwheel.results.RedirectResult;
import cn.webwheel.results.TemplateResult;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import wblog.domain.User;
import wblog.service.Md5;

import java.sql.SQLException;
import java.util.Arrays;

public class user extends BaseAction {

    public User user;

    @WebParam("user.pwd")
    public String pwd;

    public String oldpwd;

    @Action("register.html")
    public Object registerPage() {
        return new TemplateResult(this);
    }

    @Action
    public Object register() throws SQLException {

        if(!user.id.matches("^[a-zA-Z_][a-zA-Z_0-9]{2,12}$")) return err("登录名格式错误");
        if (pwd.length() < 6 || pwd.length() > 20) return err("密码长度错误");
        if (user.name.length() < 1 || user.name.length() > 20) return err("昵称长度错误");

        if (qr.query("select id from User where id=?", new ScalarHandler<Object>(), user.id) != null) {
            return err("登录名已存在");
        }
        user.pwd = Md5.md5(pwd);
        qr.update("insert into User(id, pwd, name, tags) values(?,?,?,'')",
                user.id,
                user.pwd,
                user.name);
        return ok();
    }

    @Action("login.html")
    public Object loginPage() {
        return new TemplateResult(this);
    }

    @Action
    public Object login() throws SQLException {
        byte[] md5 = qr.query("select pwd from User where id=?", new ScalarHandler<byte[]>(), user.id);
        if (md5 == null) {
            return err("用户名错误");
        }
        if (!Arrays.equals(Md5.md5(pwd), md5)) {
            return err("密码错误");
        }
        loginUserService.setLoginUserId(user.id);
        return ok();
    }

    @Action
    public Object logout() {
        loginUserService.setLoginUserId(null);
        return new RedirectResult("/");
    }

    @Action("profile.html")
    public Object profilePage() throws SQLException {
        if ((user = getLoginUser()) == null) {
            return new RedirectResult("/login.html");
        }
        return new TemplateResult(this);
    }

    @Action
    public Object update() throws SQLException {
        if (getLoginUser() == null) {
            return err("未登录");
        }

        user.pwd = qr.query("select pwd from User where id=?", new ScalarHandler<byte[]>(), getLoginUser().id);
        if (user.pwd == null) {
            return err("用户不存在");
        }

        if (user.name.length() < 1 || user.name.length() > 20) return err("昵称长度错误");

        if (!pwd.isEmpty()) {
            if (!Arrays.equals(Md5.md5(oldpwd), user.pwd)) {
                return err("密码错误");
            }

            if (pwd.length() < 6 || pwd.length() > 20) return err("密码长度错误");

            user.pwd = Md5.md5(pwd);
        }

        qr.update("update User set pwd=?, name=? where id=?", user.pwd, user.name, getLoginUser().id);

        return ok();
    }
}
